/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Dec 11, 2008, 11:56:51 AM
 */
package RTPEditor;

import java.io.File;
import javax.swing.*;

/**
 *
 * @author Zarko Coklin
 */
public class MainFrame extends javax.swing.JFrame
{

    /** Creates new form MainFrame */
    public MainFrame()
    {
        IntegerValidator validator = new IntegerValidator();
        initComponents();
        FrequencyField.setInputVerifier(validator);
        NumberOfPacketsField.setInputVerifier(validator);
        PercentField.setInputVerifier(validator);
        DistributionButtonGroup.add(NonUniformDistributionRadio);
        DistributionButtonGroup.add(UniformDistributionRadio);
    }

    public JButton getSaveButton()
    {
        return SaveButton;
    }

    public String getInputFileName()
    {
        return InputFileName.getText();
    }

    public String getOutputFileName()
    {
        return outputFileName;
    }

    public int getFrequency()
    {
        return frequency;
    }

    public int getNumDropped()
    {
        return numDropped;
    }

    public ProgressScreen getProgressScreen()
    {
        return progress;
    }

    public boolean isUniform()
    {
        return UniformDistributionRadio.isSelected();
    }

     public int getPercentage()
    {
        return percentage;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DistributionButtonGroup = new javax.swing.ButtonGroup();
        ImpairmentParameters = new javax.swing.JPanel();
        NonUniformDistribution = new javax.swing.JDesktopPane();
        Frequency = new javax.swing.JLabel();
        FrequencyField = new javax.swing.JTextField();
        NumberOfPacketsField = new javax.swing.JTextField();
        NumberOfPacketsLabel = new javax.swing.JLabel();
        NonUniformDistributionRadio = new javax.swing.JRadioButton();
        UniformPane = new javax.swing.JDesktopPane();
        PercentField = new javax.swing.JTextField();
        Percent = new javax.swing.JLabel();
        PercLost = new javax.swing.JLabel();
        UniformDistributionRadio = new javax.swing.JRadioButton();
        PcapFileSelection = new javax.swing.JPanel();
        BrowseButton = new javax.swing.JButton();
        InputFileName = new javax.swing.JTextField();
        SaveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RTPEditor - zcoklin");
        setResizable(false);

        ImpairmentParameters.setBorder(javax.swing.BorderFactory.createTitledBorder("Impairment parameters"));

        NonUniformDistribution.setBackground(javax.swing.UIManager.getDefaults().getColor("EditorPane.disabledBackground"));
        NonUniformDistribution.setBorder(javax.swing.BorderFactory.createTitledBorder("Non Uniform Distribution"));

        Frequency.setText("Frequency of dropped packets");
        Frequency.setToolTipText("<html>How often packets will be dropped.<br>\n10 - every 10th packet<br>\n20 - every 20th packet<br>\netc</html>");
        Frequency.setBounds(10, 20, 147, 14);
        NonUniformDistribution.add(Frequency, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FrequencyField.setEditable(false);
        FrequencyField.setToolTipText("<html>How often packets will be dropped.<br>\n10 - every 10th packet<br>\n20 - every 20th packet<br>\netc</html>");
        FrequencyField.setBounds(10, 40, 147, 20);
        NonUniformDistribution.add(FrequencyField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        NumberOfPacketsField.setEditable(false);
        NumberOfPacketsField.setToolTipText("Number of packets dropped");
        NumberOfPacketsField.setBounds(170, 40, 99, 20);
        NonUniformDistribution.add(NumberOfPacketsField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        NumberOfPacketsLabel.setText("Number of packets");
        NumberOfPacketsLabel.setToolTipText("Number of packets dropped");
        NumberOfPacketsLabel.setBounds(170, 20, 99, 14);
        NonUniformDistribution.add(NumberOfPacketsLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        NonUniformDistributionRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NonUniformDistributionRadioActionPerformed(evt);
            }
        });

        UniformPane.setBackground(javax.swing.UIManager.getDefaults().getColor("EditorPane.disabledBackground"));
        UniformPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Uniform Distribution"));
        UniformPane.setToolTipText("");
        PercentField.setBounds(10, 40, 50, 20);
        UniformPane.add(PercentField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Percent.setText("%");
        Percent.setBounds(70, 40, 20, 20);
        UniformPane.add(Percent, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PercLost.setText("Percent of Lost Packets");
        PercLost.setBounds(10, 20, 130, 20);
        UniformPane.add(PercLost, javax.swing.JLayeredPane.DEFAULT_LAYER);

        UniformDistributionRadio.setSelected(true);
        UniformDistributionRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UniformDistributionRadioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ImpairmentParametersLayout = new javax.swing.GroupLayout(ImpairmentParameters);
        ImpairmentParameters.setLayout(ImpairmentParametersLayout);
        ImpairmentParametersLayout.setHorizontalGroup(
            ImpairmentParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ImpairmentParametersLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(ImpairmentParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(NonUniformDistributionRadio)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ImpairmentParametersLayout.createSequentialGroup()
                        .addComponent(UniformDistributionRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(ImpairmentParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(UniformPane, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(NonUniformDistribution, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        ImpairmentParametersLayout.setVerticalGroup(
            ImpairmentParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ImpairmentParametersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ImpairmentParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ImpairmentParametersLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(UniformDistributionRadio)
                        .addGap(47, 47, 47)
                        .addComponent(NonUniformDistributionRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ImpairmentParametersLayout.createSequentialGroup()
                        .addComponent(UniformPane, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NonUniformDistribution, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        PcapFileSelection.setBorder(javax.swing.BorderFactory.createTitledBorder("Please select a valid pcap file"));

        BrowseButton.setText("Browse...");
        BrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PcapFileSelectionLayout = new javax.swing.GroupLayout(PcapFileSelection);
        PcapFileSelection.setLayout(PcapFileSelectionLayout);
        PcapFileSelectionLayout.setHorizontalGroup(
            PcapFileSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PcapFileSelectionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BrowseButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(InputFileName, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                .addContainerGap())
        );
        PcapFileSelectionLayout.setVerticalGroup(
            PcapFileSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PcapFileSelectionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PcapFileSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(InputFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BrowseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        SaveButton.setText("Save As...");
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(SaveButton)
                    .addComponent(ImpairmentParameters, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(PcapFileSelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(PcapFileSelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(ImpairmentParameters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(SaveButton)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BrowseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_BrowseButtonActionPerformed
    {//GEN-HEADEREND:event_BrowseButtonActionPerformed
        JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
        fc.setDialogTitle("Select a valid cap/pcap file");
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
        {
            InputFileName.setText(fc.getSelectedFile().getPath());
        }
    }//GEN-LAST:event_BrowseButtonActionPerformed

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_SaveButtonActionPerformed
    {//GEN-HEADEREND:event_SaveButtonActionPerformed
        SaveThread worker;

        if (isUniform() == false)
        {
            if (FrequencyField.getText() == null ||
                FrequencyField.getText().equals("") == true ||
                NumberOfPacketsField.getText() == null ||
                NumberOfPacketsField.getText().equals("") == true)
            {
                JOptionPane.showMessageDialog(this,
                    "Make sure all fields are correct and populated",
                    "Value validation",
                    JOptionPane.ERROR_MESSAGE);
                return;
            }
            frequency = Integer.parseInt(FrequencyField.getText());
            numDropped = Integer.parseInt(NumberOfPacketsField.getText());

            if (Integer.valueOf(NumberOfPacketsField.getText()) >=
                Integer.valueOf(FrequencyField.getText()))
            {
                JOptionPane.showMessageDialog(this,
                    "Number of dropped packets must be smaller than frequency",
                    "Value validation",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        else
        {
            percentage = Integer.parseInt(PercentField.getText());
            if (percentage <= 0 || percentage >= 100)
            {
                JOptionPane.showMessageDialog(this,
                    "Pecentage should be greater than 0 and less than 100",
                    "Percentage",
                    JOptionPane.ERROR_MESSAGE);
            }
        }

        JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
        fc.setDialogTitle("Save file");

        File file = null;
        if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
        {
            file = fc.getSelectedFile();
            if (file.exists() == true)
            {
                Object[] options =
                {
                    "Yes", "No"
                };
                int result = JOptionPane.showOptionDialog(this,
                    "Do you really want to overwrite an existing scenario file?",
                    "Save File",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                    null, options, options[0]);
                if (result != 0)
                {
                    return;
                }
            }
        }
        if (file == null || (file.toString().equals("") == true))
        {
            return;
        }
        outputFileName = new String(file.toString());
        worker = new SaveThread(this);
        worker.start();
        progress = new ProgressScreen(this);
        progress.setVisible(true);
    }//GEN-LAST:event_SaveButtonActionPerformed

    private void UniformDistributionRadioActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_UniformDistributionRadioActionPerformed
    {//GEN-HEADEREND:event_UniformDistributionRadioActionPerformed
        // At this point enable Uniform Distribution parameters and disable
        // Non Uniform Distribution fields
        PercentField.setEditable(true);
        FrequencyField.setEditable(false);
        FrequencyField.setText("");
        NumberOfPacketsField.setEditable(false);
        NumberOfPacketsField.setText("");
}//GEN-LAST:event_UniformDistributionRadioActionPerformed

    private void NonUniformDistributionRadioActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_NonUniformDistributionRadioActionPerformed
    {//GEN-HEADEREND:event_NonUniformDistributionRadioActionPerformed
        // Disable Uniform Distribution Parameters
        // Enable Non Uniform Distribution Parameters
        PercentField.setEditable(false);
        PercentField.setText("");
        FrequencyField.setEditable(true);
        NumberOfPacketsField.setEditable(true);

    }//GEN-LAST:event_NonUniformDistributionRadioActionPerformed

    private String outputFileName;

    private int percentage;

    private int frequency;

    private int numDropped;

    ProgressScreen progress;


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BrowseButton;
    private javax.swing.ButtonGroup DistributionButtonGroup;
    private javax.swing.JLabel Frequency;
    private javax.swing.JTextField FrequencyField;
    private javax.swing.JPanel ImpairmentParameters;
    private javax.swing.JTextField InputFileName;
    private javax.swing.JDesktopPane NonUniformDistribution;
    private javax.swing.JRadioButton NonUniformDistributionRadio;
    private javax.swing.JTextField NumberOfPacketsField;
    private javax.swing.JLabel NumberOfPacketsLabel;
    private javax.swing.JPanel PcapFileSelection;
    private javax.swing.JLabel PercLost;
    private javax.swing.JLabel Percent;
    private javax.swing.JTextField PercentField;
    private javax.swing.JButton SaveButton;
    private javax.swing.JRadioButton UniformDistributionRadio;
    private javax.swing.JDesktopPane UniformPane;
    // End of variables declaration//GEN-END:variables
}
